package com.zjdiepu.www.biz.file;

import java.io.File;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.enums.type.FFmpegOutPutType;
import com.zjdiepu.www.common.util.FileUtil;
import com.zjdiepu.www.common.util.StringUtil;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.config.FileProperty;
import com.zjdiepu.www.ibiz.base.IOtherAttachmentsService;
import com.zjdiepu.www.ibiz.file.IHandleFileService;
import com.zjdiepu.www.model.base.dto.OtherAttachments;
import com.zjdiepu.www.util.MediaCompresser;
/**
 * 项目名称 dpqc-parent-manager-file <br/> 
 * 类名称 ICompressFile <br/>  
 * 类描述 压缩处理 <br/>    
 * 创建人 xiongxiaotun <br/> 
 * 创建时间 2017年4月14日 下午9:30:32  <br/>   
 * 修改人 xiongxiaotun <br/> 
 * 修改时间 2017年4月14日 下午9:30:32   <br/>  
 * 修改备注   <br/> 
 * @version V1.0   <br/>
 */
@Service
public class HandlerFileServiceImpl implements IHandleFileService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IOtherAttachmentsService otherAttachmentsService;
	@Autowired
	private FileProperty fileProperty;
	@Override
	public boolean handle(String fileId, String srcFilePath) {
		String remarks = "";
		if(StringUtils.isBlank(fileId) || StringUtils.isBlank(srcFilePath)){
			remarks = "附件数据标识ID或文件保存路径为空";
			return false;
		}
		String basePath = fileProperty.getPath();
		String fileRealPath = basePath + srcFilePath;
		File file = new File(fileRealPath);
		if(!file.exists() || !file.isFile()) {
			remarks = "文件不存在";
			return false;
		}
		File folder = file.getParentFile();
		if(!folder.exists()) {
			folder.mkdirs();
		}
		String uuid = UUIDFactory.getUUID();
		String suffix = FileUtil.getFileSuffix(srcFilePath);
		String fileName = uuid + StringUtil.PERIOD + suffix;
		String folderPath = srcFilePath.substring(0, srcFilePath.lastIndexOf("/") + 1);
		
		String targetFilePath = folderPath + fileName;
		String targetRealPath = basePath + targetFilePath;
		File targetFile = new File(targetRealPath);
		logger.debug("target file path : {}.", targetFile.getAbsolutePath());
		long start = System.currentTimeMillis();
		//调用处理逻辑，要求只处理视频文件
		boolean process = MediaCompresser.process(FFmpegOutPutType.MP4, fileRealPath, targetRealPath);
		long end = System.currentTimeMillis();
		logger.debug("video/audio file process result : {}, cost {}ms.", process, (end - start));
		if(process) {
			OtherAttachments otherAttachments = new OtherAttachments();
			otherAttachments.setId(fileId);
			otherAttachments.setFileName(fileName); //文件名（显示名不变）
			otherAttachments.setFileHandleStatus("01"); //文件状态
			otherAttachments.setLocalFilePath(targetFilePath); //文件路径
			otherAttachments.setRemarks("压缩完成");
			otherAttachmentsService.updateJust(otherAttachments); //提交更改
			file.delete(); //删除老文件
			return true;
		} else {
			remarks = "文件压缩处理失败";
			OtherAttachments otherAttachments = new OtherAttachments();
			otherAttachments.setId(fileId);
			otherAttachments.setFileHandleStatus("01"); //文件状态
			otherAttachments.setRemarks(remarks);
			otherAttachmentsService.updateJust(otherAttachments); //提交更改
			return false;
		}
		
	}

}
